# -*- coding: utf-8 -*-
# @Time : 2025/6/25 上午9:19
# @Author : cc 
# @Email : liuchenchen@starmerx.com
# @Project : my-tools
# @Desc : 功能描述
from alibabacloud_ocr_api20210707 import models as ocr_api_20210707_models
from alibabacloud_ocr_api20210707.client import Client as ocr_api20210707Client
from alibabacloud_tea_openapi import models as open_api_models


def recognize_general_text(image_path):
    # 创建配置
    config = open_api_models.Config(
        access_key_id="LTAI5tBTfA1v1wr7Gzti4Xn3",
        access_key_secret="unwx3Hesm2hZ6e5tjRa4FyHFXoTXKZ"
    )
    config.endpoint = 'ocr-api.cn-hangzhou.aliyuncs.com'

    # 创建客户端
    client = ocr_api20210707Client(config)

    # 读取图片
    # with open(image_path, 'rb') as f:
    #     image_bytes = f.read()
    image_bytes = image_path

    # # 创建请求
    # recognize_general_request = ocr_api_20210707_models.RecognizeGeneralRequest(
    #     body=image_bytes
    # )

    recognize_all_text = ocr_api_20210707_models.RecognizeAllTextRequest(
        body=image_bytes,
        type='General'

    )

    try:
        # 发送请求
        # response = client.recognize_general(recognize_general_request)
        response = client.recognize_all_text(recognize_all_text)

        # 解析结果
        result = response.body.data
        return result
    except Exception as e:
        print(f"识别失败: {str(e)}")
        return None


# result = recognize_general_text('a.png')
# print(result)
